package com.example.cursor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.cursor.entity.User;
import com.example.cursor.mapper.UserMapper;
import com.example.cursor.service.UserService;
import com.example.cursor.dto.LoginDTO;
import com.example.cursor.exception.BusinessException;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public User login(LoginDTO loginDTO) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, loginDTO.getUsername());
        
        User user = this.getOne(queryWrapper);
        if (user == null) {
            throw new BusinessException("USER_NOT_FOUND", "用户不存在");
        }
        
        if (!user.getPassword().equals(loginDTO.getPassword())) {
            throw new BusinessException("WRONG_PASSWORD", "密码错误");
        }
        
        return user;
    }
}